package com.heima.article.controller.v1;

import com.heima.article.service.ApArticleService;
import com.heima.article.service.IApAuthorService;
import com.heima.article.service.IApCollectionService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ApArticleDto;
import com.heima.model.article.dtos.ApArticleHomeDto;
import com.heima.model.behavior.dto.BehaviorDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2023/3/14 9:47
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v1/article")
public class ApArticleController{

    @Autowired
    private ApArticleService articleService;
    @Autowired
    private IApAuthorService iApAuthorService;

    @PostMapping("/load")
    public ResponseResult load(@RequestBody ApArticleHomeDto homeDto){
        return articleService.load(homeDto, ArticleConstants.LOAD_MORE);
    }

    @PostMapping("/loadmore")
    public ResponseResult loadMore(@RequestBody ApArticleHomeDto homeDto){
        return articleService.load(homeDto,ArticleConstants.LOAD_MORE);
    }

    @PostMapping("/loadnew")
    public ResponseResult loadNew(@RequestBody ApArticleHomeDto homeDto){
        return articleService.load(homeDto,ArticleConstants.LOAD_NEW);
    }

    @PostMapping("/save")
    public ResponseResult save(@RequestBody ApArticleDto articleDto){
        return articleService.saveArticle(articleDto);
    }


    /**
     * 将文章信息传递给Comment
     */
    @GetMapping("/get_article")
    public ApArticle getArticle(@RequestParam Long id){
        return articleService.getArticle(id);
    }

    /**
     * 将自媒体端通过审核的添加至作者
     * @param userId
     * @param wmUserId
     * @return
     */
    @PostMapping("/user/save")
    ResponseResult add(@RequestParam Integer userId,
                @RequestParam Integer wmUserId,
                @RequestParam String name){
        ApAuthor apAuthor = new ApAuthor();
        apAuthor.setUserId(userId);
        apAuthor.setWmUserId(wmUserId);
        apAuthor.setCreatedTime(LocalDateTime.now());
        apAuthor.setType((short)2);
        apAuthor.setName(name);

        return iApAuthorService.add(apAuthor);
    }
}